<%--
  Created by IntelliJ IDEA.
  User: Lanan
  Date: 2017/12/26 0026
  Time: 15:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<html>
<head>
    <%@ include file="/commons/basejs.jsp" %>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>微信公众号首页Menu</title>
</head>
<body>

<script type="text/javascript">

    var wechatPublicMenuTreeGrid;
    $(function () {
        wechatPublicMenuTreeGrid = $("#wechatPublicMenuTreeGrid").treegrid({
            url:"${path}/wechat/public/number/menu/list.do",
            idField : 'id',
            treeField : 'name',
            parentField : 'parentid',
            fit : true,
            fitColumns : false,
            border : false,
            frozenColumns : [[{
                title : 'ID',
                field : 'id',
                align : 'center'
            }]],
            columns : [[{
                title : '菜单名称',
                field : 'name',
                align : 'left'
            },{
                title : '菜单类型',
                field : 'type',
                align : 'center',
                formatter : function(value) {
                    return handleType(value);
                }
            },{
                title : '菜单key',
                field : 'k',
                align : 'center'
            },{
                title : 'media_id',
                field : 'mediaId',
                align : 'center'
            },{
                title : '跳转地址',
                align : 'center',
                field : 'url',
                width : '150px'
            },{
                title : '小程序id',
                align : 'center',
                field : 'appid'
            },{
                title : '小程序地址',
                align : 'center',
                field : 'pagepath',
                width : '150px'
            },{
                title : '父类ID',
                align : 'center',
                field : 'parentid'
            },{
                title : '排序',
                align : 'center',
                field : 'ord'
            }, {
                field : 'action',
                align : 'center',
                title : '操作',
                width : 130,
                formatter : function(value, row, index) {
                    var str = '';
                    <shiro:hasPermission name="/wechat/public/number/menu/update.do">
                    str += $.formatString('<a href="javascript:void(0)" class="resource-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'" onclick="updateWechatPublicMenu(\'{0}\');" >编辑</a>', row.id);
                    </shiro:hasPermission>
                    <shiro:hasPermission name="/wechat/public/number/menu/delete.do">
                    str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                    str += $.formatString('<a href="javascript:void(0)" class="resource-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-red\'" onclick="deleteWechatPublicMenu(\'{0}\');" >删除</a>', row.id);
                    </shiro:hasPermission>
                    return str;
                }
            } ] ],
            onLoadSuccess:function(data){
                $('.resource-easyui-linkbutton-edit').linkbutton({text:'修改'});
                $('.resource-easyui-linkbutton-del').linkbutton({text:'删除'});
            },
            toolbar : '#wechatPublicMenuToolbar'
        })
    })

    function handleType(value) {
        var showType = '';
        switch (value) {
            case 'view': {
                return '页面';
            } break;
            case 'click': {
                return '按钮';
            } break;
            case 'miniprogram': {
                return '小程序';
            } break;
            case 'scancode_waitmsg': {
                return '扫码待提示';
            } break;
            case 'scancode_push': {
                return '扫码推事件';
            } break;
            case 'pic_sysphoto' : {
                return '系统拍照发图';
            } break;
            case 'pic_photo_or_album' : {
                return '拍照或者相册发图';
            } break;
            case 'pic_weixin' : {
                return '微信相册发图';
            } break;
            case 'location_select' : {
                return '发送位置';
            } break;
            case 'media_id' : {
                return '图片';
            } break;
            case 'view_limited' : {
                return '图文消息';
            } break;
        }
        return showType;
    }

    function insertWechatPublicMenu() {
        $.modalDialog({
            title : '添加微信公众号菜单',
            id:'menu_insert',
            width : 500,
            height : 350,
            href : '${path}/wechat/public/number/menu/insert/index.do',
        });
    }
    
    function updateWechatPublicMenu(id) {
        if (id != undefined) {
            wechatPublicMenuTreeGrid.treegrid('select', id);
        }
        var node = wechatPublicMenuTreeGrid.treegrid('getSelected');
        if (node == null) {
            $.messager.show({
                title:"提示",
                msg: "请选择要编辑的菜单！",
                timeout:600,style:"left:30%;top:10%",
                showType:"fade"
            });
            return;
        }
        $.modalDialog({
            title : '修改微信公众号菜单',
            id:'menu_update',
            width : 500,
            height : 350,
            href : '${path}/wechat/public/number/menu/update/index.do?id=' + id,
        });
    }
    
    function deleteWechatPublicMenu(id) {
        if (id != undefined) {
            wechatPublicMenuTreeGrid.treegrid('select', id);
        }
        lananDeleteCombotree("wechatPublicMenuTreeGrid", "您确定要删除该菜单？", "${path}/wechat/public/number/menu/delete.do");
    }

    function lananDeleteCombotree(lableId, alertStr, url) {
        var select = $('#' + lableId).treegrid('getSelected');
        if (select == null) {
            $.messager.alert("提示","请选择要删除的项！","info");
            return;
        }
        $.messager.confirm("确认",alertStr, function(yes){
            if(yes){
                $.messager.progress();
                $.ajax({
                    url:url,
                    type:"post",
                    data:{id : select.id, openid : select.openid},
                    dataType:"json",
                    success:function(data){
                        $.messager.progress("close");
                        if(data.success){
                            $.messager.show({
                                title:"删除成功",
                                msg:"删除成功!",
                                timeout:600,
                                style:"left:30%;top:10%",
                                showType:"fade"
                            });
                            $("#" + lableId).treegrid('clearSelections');
                            $("#" + lableId).treegrid("reload");
                        }else{
                            $.messager.alert("删除失败", data.msg,"error");
                        }
                    },
                    error:function(){
                        $.messager.progress("close");
                        $.messager.alert("删除失败", "服务器连接失败!","error");
                    }
                });
            }
        });
    }

    function lananUpdateOrInsertCombotree(msg, url, formId, closeId, reloadId) {
        $.messager.progress();
        $("#" + formId).form("submit",{
            url:url,
            onSubmit:function(){
                var valid = $(this).form("validate");
                if (!valid) {
                    $.messager.progress("close");
                }
                return valid;
            },
            success:function(data){
                data=$.parseJSON(data);
                $.messager.progress("close");
                if(data.success){
                    $("#" + closeId).dialog("close");
                    $.messager.show({
                        title: msg + "成功",msg: msg + "成功!",
                        timeout:600,style:"left:30%;top:10%",
                        showType:"fade"
                    });
                    $("#" + reloadId).treegrid("reload");
                    // $('#' + reloadId).datagrid('clearSelections');
                }else{
                    $.messager.alert(msg + "失败", data.msg,"error");
                }
            },
            error:function () {
                $.messager.progress("close");
                $.messager.alert("操作失败", "服务器连接失败!","error");
            }
        });
    }

    function updateToWechatPublicNumberMenu() {
        $.messager.confirm("确认","您确定同步到微信公众号首页?", function(yes){
            if(yes){
                $.messager.progress();
                $.ajax({
                    url:"${path}/wechat/public/number/menu/update/menu.do",
                    type:"post",
                    dataType:"json",
                    success:function(data){
                        $.messager.progress("close");
                        if(data.success){
                            $.messager.show({
                                title:"同步成功",
                                msg:"同步成功,您可以到微信公众号查看!",
                                timeout:600,
                                style:"left:30%;top:10%",
                                showType:"fade"
                            });
                            $("#" + lableId).treegrid('clearSelections');
                            $("#" + lableId).treegrid("reload");
                        }else{
                            $.messager.alert("同步失败", data.msg,"error");
                        }
                    },
                    error:function(){
                        $.messager.progress("close");
                        $.messager.alert("同步失败", "服务器连接失败!","error");
                    }
                });
            }
        });
    }

</script>

<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false"  style="overflow: hidden;">
        <table id="wechatPublicMenuTreeGrid"></table>
    </div>
</div>
<div id="wechatPublicMenuToolbar" style="display: none;">
    <shiro:hasPermission name="/wechat/public/number/menu/insert.do">
        <a onclick="insertWechatPublicMenu();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-plus icon-green'">添加</a>
    </shiro:hasPermission>

    <%--<shiro:hasPermission name="/wechat/public/number/menu/update/menu.do">--%>
    <shiro:hasPermission name="/wechat/public/number/menu/update/menu.do">
        <a onclick="updateToWechatPublicNumberMenu();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-redo icon-green'">同步到微信公众号</a>
    </shiro:hasPermission>
    <%--</shiro:hasPermission>--%>
</div>
</body>
</html>
